#!/bin/sh

##echo user nice system idle iowait irq softirq
CPULOG_1=$(cat /proc/stat | grep 'cpu ' | awk '{print $2" "$3" "$4" "$5" "$6" "$7" "$8}')
SYS_IDLE_1=$(echo $CPULOG_1 | awk '{print $4}')
Total_1=$(echo $CPULOG_1 | awk '{print $1+$2+$3+$4+$5+$6+$7}')

sleep 5

CPULOG_2=$(cat /proc/stat | grep 'cpu ' | awk '{print $2" "$3" "$4" "$5" "$6" "$7" "$8}')
SYS_IDLE_2=$(echo $CPULOG_2 | awk '{print $4}')
Total_2=$(echo $CPULOG_2 | awk '{print $1+$2+$3+$4+$5+$6+$7}')

SYS_IDLE=`expr $SYS_IDLE_2 - $SYS_IDLE_1`
Total=`expr $Total_2 - $Total_1`

SYS_USAGE=`expr $SYS_IDLE/$Total*100 |bc -l`
SYS_Rate=`expr 100-$SYS_USAGE |bc -l`

Disp_SYS_Rate=`expr "scale=3; $SYS_Rate/1" |bc`

exitstatus=0

if [ $Disp_SYS_Rate \> 90 ];
then
status=CRITICAL
exitstatus=2

elif [ $Disp_SYS_Rate \< 75 ];
      then
        status=OK
        exitstatus=0

elif [ $Disp_SYS_Rate \> 75 ];
      then
          status=WARNING
          exitstatus=1
      else
          status=OK
          exitstatus=0
fi

echo System CPU Rate is $Disp_SYS_Rate% [$status]
echo "| CPU_data=$Disp_SYS_Rate%;75;90;0;100"
exit $exitstatus
